package com.shareach.http.shop.rpc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.benayn.ustyle.Functional;
import com.benayn.ustyle.Reflecter;
import com.benayn.ustyle.logger.Loggers;
import com.benayn.ustyle.string.Strs;
import com.google.common.base.Function;
import com.google.common.primitives.Longs;
import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.dao.redis.impl.SnatchRDao;
import com.shareach.domain.BaseCityDomain;
import com.shareach.domain.BizTicketDomain;
import com.shareach.domain.DiscountType;
import com.shareach.http.shop.parameter.BizTicketParameter;
import com.shareach.service.BaseCityService;
import com.shareach.service.BizTicketService;
import com.shareach.util.DateUtils;
import com.shareach.util.Globals;
import com.shareach.util.HelperUtil;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAskRPC;

@Service("ticketUploadRpc")
public class TicketUploadRpc implements IAskRPC {
	
	private static final Log log = Loggers.from(TicketUploadRpc.class);
	
	@Autowired SnatchRDao snatchRDao;
	@Autowired BaseCityService cityService;
	@Autowired BizTicketService ticketService;
	@Autowired MatchRDao matchRDao;
	
	@Override
	public Answer<?> run(Ask ask, String ip) throws Exception {
		BizTicketParameter params = getParameterMap(ask);
		Answer<Long> response = this.getAnswer();
		
		if (Strs.isEmpty(params.title)) {
			response.setCode(-1);
			response.setMsg("该条数据没有title,不处理");
			log.info(String.format("该条数据没有title,不处理:::%s", ask.getSn()));
			return response;
		}
		
		snatchRDao.setSnatchedSiteId(params.city, params.site, params.siteFromId);
		
		BizTicketDomain ticketD = Reflecter.from(params).autoExchange().exchange(CITY, "city").copyTo(BizTicketDomain.class, "end", "tels");
		ticketD.setSiteId(params.siteFromId);
		
		if (null != params.end) {
			ticketD.setEnd(DateUtils.parse(params.end));
		}
		
		if (null != params.tels && !params.tels.isEmpty()) {
			BaseCityDomain city = cityService.getById(ticketD.getCity());
			List<String> tels = new ArrayList<String>();
			for (String t : params.tels) {
				if (StringUtils.isBlank(t))
					continue;
				List<String> telCodes = HelperUtil.getTels(city.getTel(), t);
				for (String tel : telCodes) {
					if (StringUtils.isBlank(tel))
						continue;
					tels.add(t);
				}
			}
			if (tels.size() > 0) {
				ticketD.setTels(tels.toArray(new String[tels.size()]));
			}
		}
		
		//shopId不能是0
		if (null != ticketD.getShopId() && 0 == ticketD.getShopId().longValue()) {
			ticketD.setShopId(null);
		}
		
		Map<String, Object> retMap = ticketService.saveOrUpdate(ticketD);
		String retCode = (String) retMap.get(Globals.GLOBAL_SERVICE_RET_CODE);
		// 匹配队列使用
		if (retCode.equals(Globals.GLOBAL_SERVICE_CODE_NOTEXIST)) {
			matchRDao.addDiscountShop(DiscountType.Ticket, (Long) retMap.get(Globals.GLOBAL_SERVICE_RET_ID));
		}
		
		return response;
	}
	
	@Override
	public Answer<Long> getAnswer() {
		return new Answer<Long>();
	}
	
	private final static Function<String, Long> CITY = new Functional<String, Long>() {

		@Override protected Long callback(String input) {
			if (null == input) {
				return null;
			}
			
			return Longs.tryParse(input);
		}
	};

	@Override
	public BizTicketParameter getParameterMap(Ask ask) {
		BizTicketParameter parameter = new BizTicketParameter();
		HashMap<String, Object> map = ask.getRequest();
		parameter.parseFromMap(map);
		return parameter;
	}

}
